//
//  FoodDetailViewController.m
//  MeiShiDa
//
//  Created by duanshiyi on 15/7/2.
//  Copyright (c) 2015年 zby. All rights reserved.
//
#define SCREEN_WIDTH    [[UIScreen mainScreen] bounds].size.width
#define SCREEN_HEIGHT   [[UIScreen mainScreen] bounds].size.height
#import "FoodDetailViewController.h"
#import <UIImageView+WebCache.h>
#import <AFNetworking.h>
#import <SVProgressHUD.h>
@interface FoodDetailViewController ()
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic) IBOutlet UIImageView *image;
@property (strong, nonatomic) IBOutlet UILabel *explainLabel;
@property (strong, nonatomic) IBOutlet UILabel *explainContentLabel;
@property (strong, nonatomic) IBOutlet UILabel *featureLabel;

@property (strong, nonatomic) IBOutlet UILabel *featureContentLabel;
@property (strong, nonatomic) IBOutlet UIButton *button;
- (IBAction)love:(UIButton *)sender;

@end

@implementation FoodDetailViewController

-(FoodModel *)foodModel{
    if (!_foodModel) {
        self.foodModel=[[FoodModel alloc]init];
    }
    return _foodModel;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self createNetworking];
    _Zan=NO;
//    [self.view setNeedsDisplay];
    [SVProgressHUD show];
    self.view.backgroundColor=[UIColor whiteColor];
    

}

-(void)createView{
    NSInteger a=[_foodModel.TeDian length];
    NSString *featureStr=[_foodModel.TeDian substringToIndex:a-10];
    
    
    self.scrollView.frame=CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-35);
    self.image.frame=CGRectMake(10, 10, SCREEN_WIDTH-20, SCREEN_WIDTH-20);
    [self.image sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.tudali.com:7177%@",_foodModel.uplodImages]]];
    self.explainLabel.frame=CGRectMake(10, SCREEN_WIDTH, 200, 25);
    
    self.explainContentLabel.frame=CGRectMake(10, SCREEN_WIDTH+30, SCREEN_WIDTH-20, [FoodDetailViewController heightFor:[_foodModel.ShuoMing stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]);
    self.explainContentLabel.text=[_foodModel.ShuoMing stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    self.featureLabel.frame=CGRectMake(10,SCREEN_WIDTH+35+[FoodDetailViewController heightFor:[_foodModel.ShuoMing stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]], 200, 25);
    self.featureContentLabel.text=featureStr;
    
    self.featureContentLabel.frame=CGRectMake(10, _featureLabel.frame.origin.y+_featureLabel.frame.size.height+5, SCREEN_WIDTH-20, [FoodDetailViewController heightFor:featureStr]);
    
    self.scrollView.contentSize=CGSizeMake(SCREEN_WIDTH, _featureLabel.frame.origin.y+_featureLabel.frame.size.height+5+[FoodDetailViewController heightFor:featureStr]);
    
    self.button.frame=CGRectMake(SCREEN_WIDTH/2-50, SCREEN_HEIGHT-30, 100, 25);
    [_button setTitle:[NSString stringWithFormat:@"赞  (%d)",[_foodModel.Zan intValue]] forState:UIControlStateNormal];
    
}

//返回LAbel的高度   外界用不到   不用声明
+(CGFloat)heightFor:(NSString *)str{
    //影响高度的有两个因素：1.字体  2.宽度
    //使用 boundingRectWithSize：option：attributes:context方法来计算高度。
    NSDictionary *dic=[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:15] forKey:NSFontAttributeName];
    CGRect bounds=[str  boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-20, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil ];
    return  bounds.size.height;
}

//请求数据
-(void)createNetworking{
    NSString *urlStr =@"http://www.tudali.com:7177/App/CaiDan/getContent";
    NSString *otherStr=[NSString stringWithFormat:@"ContentId=%@",self.ID];
  
    //    NSString *URLTmp1 = [URLTmp stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  //转码成UTF-8  否则可能会出现错误

    NSString *url=[NSString stringWithFormat:@"%@?%@",urlStr,otherStr];
    NSLog(@"%@",url);
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: url]];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *requestTmp = [NSString stringWithString:operation.responseString];
        NSData *resData = [[NSData alloc] initWithData:[requestTmp dataUsingEncoding:NSUTF8StringEncoding]];
        //系统自带JSON解析
        NSDictionary *dic= [NSJSONSerialization JSONObjectWithData:resData options:NSJSONReadingMutableLeaves error:nil];
        
        NSDictionary *dict=dic[@"jsondata"];

        [self.foodModel setValuesForKeysWithDictionary:dict];
        self.navigationItem.title=self.foodModel.MingCheng;
        [self createView];
        [SVProgressHUD dismiss];
    
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Failure: %@", error);
        [SVProgressHUD dismiss];
    }];
    [operation start];
}




- (IBAction)love:(UIButton *)sender {
    NSString *urlStr =@"http://www.tudali.com:7177/App/CaiDan/Zan";
    NSString *otherStr=[NSString stringWithFormat:@"ContentId=%@",_foodModel.ID];
    
    //    NSString *URLTmp1 = [URLTmp stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  //转码成UTF-8  否则可能会出现错误
    
//    NSString *otherStr1=[otherStr stringByReplacingOccurrencesOfString:@"Start=0" withString:[NSString stringWithFormat:@"Start=%li",_start]];
     if (_Zan==NO) {
         
    NSString *url=[NSString stringWithFormat:@"%@?%@",urlStr,otherStr];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: url]];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
            [SVProgressHUD showSuccessWithStatus:@"点赞成功" maskType:SVProgressHUDMaskTypeGradient];
            [self.button setTitle:[ NSString stringWithFormat:@"赞  (%d)",[_foodModel.Zan intValue]+1] forState:UIControlStateNormal];
            _Zan=YES;
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Failure: %@", error);
            [SVProgressHUD dismiss];
            }];
            [operation start];
    }else{
            [SVProgressHUD showErrorWithStatus:@"不能重复点赞" maskType:SVProgressHUDMaskTypeGradient];
        }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
